<?php

namespace backend\modules\orders\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\orders\models\OrderInfo;

/**
 * OrderInfoSearch represents the model behind the search form about `backend\modules\orders\models\OrderInfo`.
 */
class OrderInfoSearch extends OrderInfo
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id','order_id', 'order_sn', 'consignee', 'address', 'tel', 'mobile', 'email', 'best_time',  'shipping_name', 'pay_name', 'referer', 'pay_time', 'invoice_no', 'to_buyer', 'transaction_id', 'shopping_temp'], 'safe'],
            [['created_at', 'order_status', 'shipping_status', 'pay_status', 'country', 'province', 'city', 'district', 'shipping_id', 'pay_id', 'add_time', 'confirm_time', 'shipping_time', 'parent_id', 'updated_at', 'comment_status', 'is_delete', 'is_without_pay', 'cancel_status'], 'integer'],
            [['goods_amount', 'shipping_fee', 'pay_fee', 'money_paid',  'order_amount', 'discount', 'order_check_money', 'order_user_money', 'order_should_check_money'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = OrderInfo::find();
        $query->orderBy(['pay_time'=>SORT_DESC]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $params = isset($params['OrderInfoSearch'])?$params['OrderInfoSearch']:Yii::$app->request->post();
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
//            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'user_id' => $this->user_id,
            'order_status' => $this->order_status,
            'shipping_status' => $this->shipping_status,
            'pay_status' => $this->pay_status,
            'country' => $this->country,
            'province' => $this->province,
            'city' => $this->city,
            'district' => $this->district,
            'shipping_id' => $this->shipping_id,
            'pay_id' => $this->pay_id,
            'goods_amount' => $this->goods_amount,
            'shipping_fee' => $this->shipping_fee,
            'pay_fee' => $this->pay_fee,
            'money_paid' => $this->money_paid,
            'order_amount' => $this->order_amount,
//            'add_time' => $this->add_time,
            'confirm_time' => $this->confirm_time,
            'shipping_time' => $this->shipping_time,
            'parent_id' => $this->parent_id,
            'discount' => $this->discount,
            'updated_at' => $this->updated_at,
            'comment_status' => $this->comment_status,
            'is_delete' => $this->is_delete,
            'order_check_money' => $this->order_check_money,
            'order_user_money' => $this->order_user_money,
            'order_should_check_money' => $this->order_should_check_money,
            'is_without_pay' => $this->is_without_pay,
            'cancel_status' => $this->cancel_status,
        ]);
        if($this->add_time) {
            $add_time = strtotime($this->add_time);
            $query->andWhere(['>','add_time',$add_time]);
            $query->andWhere(['<','add_time',$add_time+3600*24]);
        }

        $query->andFilterWhere(['like', 'order_id', $this->order_id])
            ->andFilterWhere(['like', 'order_sn', $this->order_sn])
            ->andFilterWhere(['like', 'consignee', $this->consignee])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'tel', $this->tel])
            ->andFilterWhere(['like', 'mobile', $this->mobile])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'best_time', $this->best_time])
            ->andFilterWhere(['like', 'shipping_name', $this->shipping_name])
            ->andFilterWhere(['like', 'pay_name', $this->pay_name])
            ->andFilterWhere(['like', 'referer', $this->referer])
            ->andFilterWhere(['like', 'pay_time', $this->pay_time])
            ->andFilterWhere(['like', 'invoice_no', $this->invoice_no])
            ->andFilterWhere(['like', 'to_buyer', $this->to_buyer])
            ->andFilterWhere(['like', 'transaction_id', $this->transaction_id])
            ->andFilterWhere(['like', 'shopping_temp', $this->shopping_temp]);

        return $dataProvider;
    }
}
